package com.aiyige.page.selectinterest.adapter;

import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.selectinterest.SelectInterestPage;
import com.aiyige.page.selectinterest.model.IndustryItem;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/10/25.
 */

public class IndustryAdapter extends BaseQuickAdapter<IndustryItem, IndustryAdapter.IndustryViewHolder> {
    public IndustryAdapter() {
        super(R.layout.industry_item, new LinkedList<IndustryItem>());
    }

    @Override
    protected void convert(IndustryViewHolder helper, IndustryItem item) {
        helper.bindData(item);
    }

    public class IndustryViewHolder extends BaseViewHolder {
        IndustryItem data;
        @BindView(R.id.bar)
        View bar;
        @BindView(R.id.block)
        View block;
        @BindView(R.id.titleTv)
        TextView titleTv;

        public IndustryViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(IndustryItem industryItem) {
            data = industryItem;
            if (data.isStats()) {
                titleTv.setTypeface(null, Typeface.BOLD);
                int selectedNum = 0;
                for (IndustryItem item : getData()) {
                    selectedNum += item.getSelectedInterestItemList().size();
                }
                titleTv.setText(itemView.getContext().getString(R.string.selected_industry) + "(" + selectedNum + "/" + SelectInterestPage.MAX_SELECT_NUM + ")");
                if (data.isSelected()) {
                    bar.setVisibility(View.VISIBLE);
                    block.setVisibility(View.VISIBLE);
                } else {
                    bar.setVisibility(View.INVISIBLE);
                    block.setVisibility(View.INVISIBLE);
                }
            } else {
                titleTv.setTypeface(null, Typeface.NORMAL);
                if (data.isSelected()) {
                    bar.setVisibility(View.VISIBLE);
                    block.setVisibility(View.VISIBLE);
                } else {
                    if (data.getSelectedInterestItemList().isEmpty()) {
                        bar.setVisibility(View.INVISIBLE);
                    } else {
                        bar.setVisibility(View.VISIBLE);
                    }
                    block.setVisibility(View.INVISIBLE);
                }
                titleTv.setText(data.getTitle());
            }
        }
    }
}
